package view;


import interfacePack.ITableObserver;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import dao.Excel03DAO;
import dao.Excel07DAO;
import exception.ViewException;

import model.BankInfoTableModel;
import model.PrepayRecordItem;
import model.TradeRecordTableModel;

public class ReserveDataAccessPanel extends JPanel implements ITableObserver{

	private static final long serialVersionUID = 1L;

	private BankInfoTableModel bankInfoTableModel = new BankInfoTableModel();
	private TradeRecordTableModel tradeRecordTableModel = new TradeRecordTableModel();

	private JPanel bankInfoImportPanel = new JPanel(new BorderLayout());
	private JPanel tradeRecordImportPanel = new JPanel(new BorderLayout());
	
	//上半部分，银行信息导入
	private JLabel upAddressLabel = new JLabel("文件地址: ");
	private JTextField bankInfoFiledir = new JTextField(30);
	private JButton upChooseBn = new JButton("选择文件");
	private JButton uploadBn = new JButton("加载数据");
	private JTable bankInfoTable = new JTable();
	private JScrollPane bankInfoScr;
	private JPanel bankInfoButtPanel = new JPanel(new FlowLayout());
	private JButton addBank = new JButton("增加");
	private JButton modifyBank = new JButton("修改");
	private JButton deleteBank = new JButton("删除");
	
	//下半部分，交易信息导入
	private JLabel downAddressLabel = new JLabel("文件地址: ");
	private JTextField tradeRecordFiledir = new JTextField(30);
	private JButton downChooseBn = new JButton("选择文件");
	private JButton downuploadBn = new JButton("加载数据");
	private JTable tradeRecordTable = new JTable();
	private JScrollPane tradeRecordScr;
	
	
	private JPanel rightTopTextFieldPanelBn = new JPanel(new FlowLayout(
			FlowLayout.RIGHT));
	private JPanel rightDownTextFieldPanelBn = new JPanel(new FlowLayout(
			FlowLayout.RIGHT));
	
	private ArrayList<PrepayRecordItem> all = new ArrayList<PrepayRecordItem>();

	public ReserveDataAccessPanel() {
		
//		//observer订阅DAO
//		Excel03DAO.register(this);
//		Excel07DAO.register(this);
		
		setLayout(new GridLayout(2,1));
		initBankInfoPanel();
		initTradeRecordImportPanel();
	
		add(bankInfoImportPanel);
		add(tradeRecordImportPanel);
		
	}

	private void initBankInfoPanel() {
		bankInfoFiledir.setForeground(Color.BLUE);
		upChooseBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String dir = chooseFile();
				bankInfoFiledir.setText(dir);
			}
		});
		uploadBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				try {
					uploadFile(bankInfoFiledir.getText());
				} catch (ViewException e) {
					JOptionPane.showMessageDialog(uploadBn, e.getMessage());
				}
			}
		});
		rightTopTextFieldPanelBn.add(upChooseBn);
		rightTopTextFieldPanelBn.add(uploadBn);

		JPanel upperBankInfoPanel = new JPanel(new BorderLayout());
		upperBankInfoPanel.setBorder(new TitledBorder("导入银行信息数据"));
		upperBankInfoPanel.add(upAddressLabel,BorderLayout.WEST);
		upperBankInfoPanel.add(bankInfoFiledir);
		upperBankInfoPanel.add(rightTopTextFieldPanelBn,BorderLayout.EAST);
		bankInfoImportPanel.add(upperBankInfoPanel, BorderLayout.NORTH);
		
		bankInfoTable.setSelectionBackground(Color.ORANGE);
		bankInfoTable.setSelectionForeground(Color.blue);
		bankInfoTable.setRowHeight(20);
		bankInfoTable.setModel(bankInfoTableModel);
		bankInfoScr = new JScrollPane(bankInfoTable);
		bankInfoImportPanel.add(bankInfoScr, BorderLayout.CENTER);
		
		bankInfoButtPanel.add(addBank);
		bankInfoButtPanel.add(modifyBank);
		bankInfoButtPanel.add(deleteBank);
		bankInfoImportPanel.add(bankInfoButtPanel, BorderLayout.SOUTH);
		
		
	}

	private void initTradeRecordImportPanel() {
		
		tradeRecordFiledir.setForeground(Color.BLUE);
		downChooseBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String dir = chooseFile();
				tradeRecordFiledir.setText(dir);
			}
		});
		downuploadBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				try {
					uploadFile(tradeRecordFiledir.getText());
				} catch (ViewException e) {
					JOptionPane.showMessageDialog(downuploadBn, e.getMessage());
				}
			}
		});
		rightDownTextFieldPanelBn.add(downChooseBn);
		rightDownTextFieldPanelBn.add(downuploadBn);

		JPanel downTradeRecordPanel = new JPanel(new BorderLayout());
		downTradeRecordPanel.setBorder(new TitledBorder("导入转账交易记录"));
		downTradeRecordPanel.add(downAddressLabel, BorderLayout.WEST);
		downTradeRecordPanel.add(tradeRecordFiledir);
		downTradeRecordPanel.add(rightDownTextFieldPanelBn, BorderLayout.EAST);
		tradeRecordImportPanel.add(downTradeRecordPanel, BorderLayout.NORTH);
		
		tradeRecordTable.setSelectionBackground(Color.ORANGE);
		tradeRecordTable.setSelectionForeground(Color.blue);
		tradeRecordTable.setRowHeight(20);
		tradeRecordTable.setModel(tradeRecordTableModel);
		tradeRecordScr = new JScrollPane(tradeRecordTable);
		tradeRecordImportPanel.add(tradeRecordScr, BorderLayout.CENTER);
		
		
		

	}

	// 选择文件.xlsx或.xls文件
	private String chooseFile() {
		String dir = "";
		Component parent = null; // Dialog上级组件
		JFileChooser chooser = new JFileChooser(".");

		javax.swing.filechooser.FileFilter dirFilter = new javax.swing.filechooser.FileFilter() {
			public boolean accept(File f) {
				if (f.getName().endsWith(".xlsx")
						|| f.getName().endsWith(".xls") || f.isDirectory()) {
					return true;
				}
				return false;
			}

			public String getDescription() {
				return "Excel文件（.xlsx或.xls）";
			}
		};

		chooser.setFileFilter(dirFilter);
		if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
			dir = chooser.getSelectedFile().getAbsolutePath();
		}

		return dir;
	}

	// todo::上传文件,dir是文件路径
	private void uploadFile(String dir) throws ViewException{

		ExcelAccess excelAccess = new ExcelAccess(dir);
		// 上传成功后需要更新table中的数据： prepayTableModel.addAllRecords((Collection)all);

	}

	public void update(ArrayList<String> cells) {
// todo::加载所有records到prepayRecordTable
		PrepayRecordItem item = new PrepayRecordItem();
		item.setRow(cells.get(0), cells.get(1), cells.get(2), cells.get(3), cells.get(4));
		all.add(item);
	}

	public void updateFinished() {
		// TODO Auto-generated method stub
		
	}

//	public void updateFinished() {
//		bankInfoTableModel.addAllRecords(all);
//		prepayRecordTable.setModel(bankInfoTableModel);
//		scr.repaint();
//		all.clear();
//	}
}
